<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version= "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" encoding="UTF-8"/>

  <xsl:template match="/">
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html>
    </xsl:text>
    <html lang="en">
      <head>
        <link href="VBADoc.css" rel="stylesheet" type="text/css" />
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
    
  
  <xsl:template match="VBADoc">
    <header>
      <h1>Documentation for <xsl:value-of select="WorkbookName"/></h1>
      <h2>VBProject: <xsl:value-of select="VBProjectName"/></h2>
      <b>Date: <xsl:value-of select="CreationDate"/></b>
    </header>
    
    <nav class="line">
      <b>jump to: </b>
      <ul>
        <li><a href="#ClassModules">Class Modules</a></li>
        <li><a href="#Modules">Code Modules</a></li>
        <li><a href="#Documents">Documents (Worksheets)</a></li>
        <li><a href="#Forms">User Forms</a></li>
      </ul>
    </nav>
    
    <div id="CodeComponents">
      <xsl:apply-templates select="CodeComponents"/>
    </div>
        
    <footer>generated with VBADoc version <xsl:value-of select="@Version"/></footer>
  </xsl:template>
  
  
  <xsl:template match="CodeComponents">
  
    <div id="ClassModules">
      <h3>Class Modules</h3>
      <xsl:apply-templates select="ClassModules"/>
    </div>
    
    <div id="CodeModules">
      <h3 id="Modules">Code Modules</h3>
      match "Modules"
    </div>

    <div id="Documents">
      <h3>Document (Worksheets)</h3>
      <xsl:apply-templates select="Documents"/>
    </div>

    <div id="Forms">
      <h3>User Forms</h3>
      select "Forms"
    </div>
  </xsl:template>


  <xsl:template match="CodeComponents/*">
    <nav class="line">
      <b>jump to:</b>
      <ul>
        <xsl:for-each select="child::*">
          <li><a href="#{name(..)}.{Name}"><xsl:value-of select="Name"/></a></li>
        </xsl:for-each>
      </ul>
    </nav>
    
    <xsl:apply-templates/>
  </xsl:template>
  
  
  <xsl:template match="ClassModule|Document|Form|Module">   
    <div id="{name(..)}.{Name}" class="{name()}">
      <h4><xsl:value-of select="Name"/></h4>
      <xsl:apply-templates select="CommentHead"/> <!-- also checks if CommentHead exists -->
      
      <!-- only create navigation if more than 3 Procedures -->
      <xsl:if test="count(Sub|Function)>3">
        <nav class="line">
          <b>jump to:</b>
          <ul>
            <xsl:for-each select="child::*">
              <li><a href="#{name(../..)}.{../Name}.{Name}"><xsl:value-of select="Name"/></a></li>
            </xsl:for-each>
          </ul>
        </nav>
      </xsl:if>
      
      <xsl:apply-templates select="Sub|Function"/>
      
      <!--div class="back">
        <b>jump to: </b>
        <a href="#{name(..)}.{Name}"> <xsl:value-of select="Name"/></a> | 
        <a href="#{name(..)}"><xsl:value-of select="name(..)"/></a> |
        <a href="#CodeComponents">CodeComponents</a>
      </div-->
    </div>
  </xsl:template>
  
  <xsl:template match="Sub|Function">
    <div id="{name(../..)}.{../Name}.{Name}" class="{name()}">
      <b><xsl:value-of select="Name"/></b><br/>
      <b>Declaration: </b><xsl:value-of select="Declaration"/><br/>
      <xsl:apply-templates select="CommentHead"/>
      
      <div class="back">
        <b>jump to: </b>
        <a href="#{name(../..)}.{../Name}"><xsl:value-of select="../Name"/></a> |
        <a href="#{name(../..)}"><xsl:value-of select="name(../..)"/></a> |
        <a href="#CodeComponents">CodeComponents</a>
      </div>
    </div>
  </xsl:template>
  
  
  <xsl:template match="CommentHead">
    <xsl:if test=".!=''">
     <code class="CommentHead">
       <xsl:for-each select="Line">
         <xsl:if test=".!=''"><xsl:value-of select="."/><br /></xsl:if> 
       </xsl:for-each>
     </code>
    </xsl:if>
  </xsl:template>
  
  
  
</xsl:stylesheet>